package cn.zr.aitool.finger;

import android.app.Activity;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import cn.zr.aitool.R;
import cn.zr.aitool.utils.DpUtil;

/**
 * 文件名称: ${name}
 * 功能描述:
 *
 * @author:${user} 创建时间: ${data} ${time}
 * 责任人:
 * 备份责任人:
 * 修改人:
 * 修改内容:
 * 修改时间:${data} ${time}
 * Copyright (C) 2018 猎户星空
 */
public class FingerPrintProvider {
    private final static int DIALOG_SIZE = 240;
    private Activity mContext;
    private AlertDialog mDialog;

    public FingerPrintProvider(Activity context) {
        this.mContext = context;
    }

    public void onFingerprintClick() {

        FingerprintUtil.callFingerPrint(new FingerprintUtil.OnCallBackListenr() {

            @Override
            public void onSupportFailed() {
                showToast("当前设备不支持指纹");
            }

            @Override
            public void onInsecurity() {
                showToast("当前设备未处于安全保护中");
            }

            @Override
            public void onEnrollFailed() {
                showToast("请到设置中设置指纹");
            }

            @Override
            public void onAuthenticationStart() {
                showConfirmDialog();
            }

            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                showToast(errString.toString());
                if (mDialog != null && mDialog.isShowing()) {
                    mDialog.dismiss();
                }
            }

            @Override
            public void onAuthenticationFailed() {
                showToast("解锁失败");
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                showToast(helpString.toString());
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                showToast("解锁成功");
                if (mDialog != null && mDialog.isShowing()) {
                    mDialog.dismiss();
                }
            }
        }, mContext);
    }

    private void showConfirmDialog() {
        //设置对话框的按钮
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.finger_dialog, null);
        ImageView line = view.findViewById(R.id.line);

        TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(mContext, R.anim.finger_checking);
        line.startAnimation(translateAnimation);
        builder.setView(view);
        mDialog = builder.create();
        mDialog.show();

        android.view.WindowManager.LayoutParams p = mDialog.getWindow().getAttributes();
        p.height = DpUtil.dp2px(mContext, DIALOG_SIZE);
        p.width = DpUtil.dp2px(mContext, DIALOG_SIZE);
        mDialog.getWindow().setAttributes(p);
    }

    private void showToast(String name) {
        Toast.makeText(mContext, name, Toast.LENGTH_SHORT).show();
    }

    public void closeDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

}
